package com.kx.smsplantform.job;

import com.kx.smsplantform.client.CacheClient;
import com.kx.smsplatform.common.constants.CacheConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * 监控用户的账户余额，余额不足给用户发送短信
 *
 * @Author Sun
 * @Date 2021/7/7 19:26
 */
@Slf4j
@Component
public class MonitorBalanceJob implements SimpleJob {
    @Autowired
    private CacheClient cacheClient;

    @Override
    public void execute(ShardingContext shardingContext) {
        //使用通配符获取所有的key
        log.info("【监控模块 监听用户余额】监听用户余额开始执行");
        Set<String> keys = cacheClient.hGetKeys(CacheConstants.CACHE_PREFIX_CLIENT+"*");
        for (String key : keys) {
            Map map = cacheClient.hMGet(key);
            Integer id = (Integer) map.get("id");
            String corpname = (String) map.get("corpname");
            Integer fee = (Integer) map.get("fee");
            if (fee < 1000) {
                log.warn("【监控模块 监听用户余额】 用户余额不足！ 用户id={}，corpname={}，余额为fee={}",id, corpname, fee);
                //TODO 需要给用户发送短信让用户尽快充值
            }
        }
    }
}
